/**
 * http://code.google.com/p/gwt-spot
 * 
 * GWT-Spot -- Copyright 2008-2009
 *          -- by Dunkel, Ricardo
 *  
 */
package de.ricardodunkel.gwtspot.client.api.ui.common;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

/**
 * Common button widget utility for easier button creation.
 * 
 * @author Dunkel, Ricardo
 * 
 * @version 0.1
 * 
 */
public class ButtonUtility extends WidgetUtility {

	/**
	 * Does create an <em>standard</em> new button.
	 * 
	 * @param title  A button title used within, <em>HTML</em> composed.
	 * 
	 * @return A new {@link Button} object.
	 */
	public static Button createButton(String title) {
		return createButton(title, null, null);
	}
	
	/**
	 * Does create an <em>basic</em> new button.
	 * 
	 * @param title  A button title used within, <em>HTML</em> composed.
	 * @param text  An button text to set.
	 * 
	 * @return A new {@link Button} object.
	 */
	public static Button createButton(String title, String text) {
		return createButton(title, text, null);
	}
	
	/**
	 * Does create an <em>extended</em> new button.
	 * 
	 * @param title  A button title used within, <em>HTML</em> composed.
	 * @param text  An button text to set.
	 * @param style  A style to set for button, if desired.
	 * 
	 * @return A new {@link Button} object.
	 */
	public static Button createButton(String title, String text, String style) {
		Button bb = new Button();
		setWidgetProperties(bb, new HTML(title).getHTML(), style);
		setWidgetText(bb, text);
		return bb;
	}
	
	/**
	 * Generate a close button binded to a dialog box.
	 * 
	 * <p>
	 * 	Just add this button to a desired place after
	 *  method usage.
	 * </p>
	 * 
	 * @param title  A description of close button.
	 * @param db  A dialog box a button is related to.
	 * 
	 * @return  A ready to use button.
	 */
	public static Button createCloseButton(String title, final DialogBox db) {
		Button b = createButton(title, title);
		b.addClickListener( new ClickListener() {
			public void onClick(Widget arg0) {
				if( null != db && db.isVisible() ) {
					db.hide();
				}
			}
		});
		return b;
	}
	
}
